<?php


namespace app\service\order;


use app\common\exception\ApiException;
use app\model\cart\VerifyCartModel;
use app\model\goods\OrderModel;
use app\model\partner\EscortMenuOrderModel;
use app\model\partner\EscortMenuReserveOrderModel;
use app\model\partner\UserPartnerModel;
use app\service\user\PartnerMoneyRecordService;
use app\service\user\UserService;
use think\facade\Db;

class VerifyOrderService
{

    /**
     * @param $id
     * @param int $coachAmount 验车订单完成才会写入教练工资
     * @return bool
     * @throws ApiException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function completeOrderService($id, $coachAmount = 0)
    {
        $info = EscortMenuReserveOrderModel::find($id);
        if (!$info) {
            throw new ApiException('订单不存在');
        }
        if ($info['status'] != 3 && $info['status'] != 7) {
            throw new ApiException('订单状态错误');
        }
        $info->status = 5;
        $info->complete_time = time();
        $info->save();

        // 用户订单完成
        $orderInfo = OrderModel::where('order_sn', $info['order_sn'])->find();
        if ($orderInfo) {
            $orderInfo->order_status = 1;
            $orderInfo->save();
        }

        $userPartnerInfo = UserPartnerModel::find($info['coach_user_id']);
        if ($info['order_type'] == 2) {
            // 验车订单 查询教练工资
            $menuOrderInfo = EscortMenuOrderModel::find($info['order_id']);
            if ($menuOrderInfo['source_id'] == 1) {
                // 小程序下单 已经知道教练工资
                $amount = $menuOrderInfo['coach_amount'];
            } else {
                // 后台加单 需要设置教练工资
                if ($coachAmount <= 0) {
                    throw new ApiException('请设置教练工资');
                }
                if ($userPartnerInfo['check_car_amount'] > 0) {
                    $amount = intval($coachAmount * $userPartnerInfo['check_car_amount'] / 100);
                } else {
                    throw new ApiException('请设置教练工资比例');
                }
                $menuOrderInfo->coach_amount = $amount;
                $menuOrderInfo->pay_amount = $coachAmount;
                $menuOrderInfo->amount = $coachAmount;
                $menuOrderInfo->save();
            }
        } else {
            // 陪驾订单
            if ($info['is_trial'] == 0) {
                // 正式课
                $amount = bcmul($userPartnerInfo['class_amount'], $info['duration'], 2);
            } else {
                // 体验课
                $amount = bcmul($userPartnerInfo['trial_class_amount'], $info['duration'], 2);
            }
            $menuOrderInfo = EscortMenuOrderModel::find($info['order_id']);
            $menuOrderInfo->coach_amount = $amount;
            $menuOrderInfo->save();
        }
        // 教练加钱
//        if ($amount > 0) {
            $partnerMoneyRecordService = new PartnerMoneyRecordService();
            $type = PartnerMoneyRecordService::VERIFY_CART;
            $title = $partnerMoneyRecordService->getTypeText($type);
            $partnerMoneyRecordService->createLog($title, $info['coach_user_id'], $type, $amount, 0, $info['order_sn']);
//        }

        // 教练增加服务次数
        $userPartnerInfo->service_num = Db::raw('service_num+1');
        $userPartnerInfo->accept_order_num = Db::raw('accept_order_num+1');
        $userPartnerInfo->save();

        // 推荐奖励
        $menuOrderInfo = EscortMenuOrderModel::find($info['order_id']);
        $userService = new UserService();
        $userService->award($menuOrderInfo['user_id'], $menuOrderInfo['amount'], $menuOrderInfo['order_sn']);
        return true;
    }
}